<template>
  <el-table :data="tableData" :key="tableKey" style="width: 100%; height: 90%">
    <template v-for="(item, index) in tableConfig" :key="index">
      <tableColunm :item="item" :tableData="tableData"   :table-rows="total" @updateTable="getTableData"></tableColunm>
    </template>
  </el-table>
  <div class="sc-table-select__page">
    <el-pagination
      small="true"
      :total="total"
      v-model:page-size="pageSize"
      v-model:current-page="pageNo"
      :page-sizes="tablePage.pageSize"
      layout="jumper,total,sizes,prev, next"
    >
    </el-pagination>
  </div>
</template>
<script setup>
import { ref, onMounted, onUnmounted, watch, nextTick } from "vue";
import Api from "@/api/model/system";
import tableColunm from "@/views/report/column.vue";
const tableData = ref([]);
const tableConfig = ref([]);
const total = ref(0);
const tableKey = ref(0);
const pageSize = ref(20);
const tablePage = ref({})
const pageNo = ref(1);
const fuzzyQuery = ref(true);
const form = ref({})
const flexColumnWidth = ref(true)
const props = defineProps({
  widget: { type: Object, default: () => {} },
  fileName: { type: String, default: "" },
});
watch([pageSize,pageNo],()=>{
  getTableData()
})
watch(()=> props.widget?.datasetId,()=>{
  getInit()
},{deep:false})
onMounted(() => {
  getInit();
});
const getInit = async () => {
  let params = {
    fileName: props.fileName,
    datasetId:props.widget.datasetId
  };
  console.log(params)
  let res = await Api.report.selectCompanyList.post(params);
  if (res.code == 200) {
    tableConfig.value = res.bean.table.columns; //表格列
    tablePage.value = res.bean.table.page;
    flexColumnWidth.value = res.bean.table.flexColumnWidth;
    if (res.bean.table.page !== null) {
      pageSize.value = res.bean.table.page.page;
    }
    //判断是否初始化显示表格数据
    nextTick(() => {
      getTableData();
    });
  }
};
const getTableData = async (val) => {
  if(val){
    form.value = val
  }
  var params = {
    fileName: props.fileName,
    fieldMap: form.value,
    datasetId:props.widget.datasetId,
    queryPattern:'designPattern',
    fuzzyQuery: fuzzyQuery.value,
    pageSize: pageSize.value,
    pageNo: pageNo.value, //1
  };
  var res = await Api.select.query.post(params);
  if (res.code == 200) {
    tableConfig.value = res.bean.table.columns;
    total.value = res.rows;
    tableData.value = res.reportData;
    tableKey.value += 1;
  }
};
defineExpose({
  getTableData
})
</script>